
// one line to give the library's name and an idea of what it does.
// Copyright (C) year  name of author
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing;
namespace MyCodeGeneration.Data
{
	/// <summary>
	/// Description of MysqlData.
	/// </summary>
	public class MysqlData		

	{
		#region // campos
		private MySqlConnection		conn;
		private DataTable			data;
		private MySqlDataAdapter	da;		
		private MySqlCommandBuilder	cb;
		MySqlDataReader reader;
		private string myConnection;
		

		public MysqlData(){			
			init();
		}
		public MysqlData(String conn){
			myConnection = conn;
			init();
		}
		public void init(){
			conn = null;
			reader = null;
		}
		#endregion
		
		public void Connect(){
			if (isConnect())
				Disconnect();
			try{				   
				conn = new MySqlConnection(myConnection);				
				conn.Open();								
			}catch(MySqlException ex){
				conn = null;								
				MessageBox.Show(ex.Message);				
			}	
			
		}
				
		public bool isConnect(){			
			return  (conn != null);			
		}

		public void Disconnect(){
			if (isConnect()){
				conn.Close();
				conn = null;
			}else
				MessageBox.Show("No existe conexion p/ desconectar");							
		}		
		
		public MySqlDataReader Simplequery(string s){
			MySqlCommand cmd = new MySqlCommand(s, conn);
			try {
				reader = cmd.ExecuteReader();				
			}
			catch (MySqlException ex) {
				MessageBox.Show(ex.Message);}
			finally {				
			}
			return reader;					
		}

		public DataTable query(string s){
			//MessageBox.Show(s);
			data = new DataTable();
			da = new MySqlDataAdapter(s, conn );			
			cb = new MySqlCommandBuilder( da );			
			da.Fill( data );
			return data;
		}
		
		public void StoredProcedure(params string[] args){
			string sql = "CALL " + args[0] + "( ";
			if (args.Length>1){
				for (int i = 1; i<(args.Length-1);i++)
				 		sql = sql + args[i] + ", ";								
				sql += args[args.Length-1] +" )";				
			}else
				sql+=")";			
			query(sql); 
		}
		
		public void nonQuery(string s){
			//MessageBox.Show(s);
			MySqlCommand cmd = new MySqlCommand(s, conn);
			try {
				cmd.ExecuteNonQuery();			
			}
			catch (MySqlException ex){
				MessageBox.Show(ex.Message);
			}
		}					
		
		public DataTable estructura(string table){
			MySqlCommand myCommand = new MySqlCommand("SELECT * FROM "+table+"",conn);			
			MySqlDataReader myReader = myCommand.ExecuteReader(System.Data.CommandBehavior.SchemaOnly);			
			//MySqlDataReader myReader = myCommand.ExecuteReader(System.Data.CommandBehavior.Default);
			DataTable d =  myReader.GetSchemaTable();
			myReader.Close();
			return d;
			
		}
		#region //Propiedades
		public string MyConnection {
			get {
				return myConnection;
			}
			set {
				myConnection = value;
			}
		}
		#endregion
	}
}
